﻿Imports System.Math

Public Class Form1

    Dim WithEvents Scene As New Scene
    Dim WithEvents Timer As New Timer With {.Interval = 25}

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)
        Show()
        'CreateRabbit()
        Dim l = 0.3
        Dim r1 = 0.5
        Dim r2 = r1
        Dim w = 0.0
        Dim ol = l
        Dim B = Scene.Add(New Box(10, 1, 10))
        B.Offset(0, -5, 0)
        B.Material.Ambient = Color.Black
        Do
            'Dim S = Scene.Add(New Cylinder(l, r1, r2))
            'S.Offset(0, 0, w)
            'Scene.Shapes.Add(S)
            r1 = r2
            r2 *= 0.6
            If r2 < 0.005 Then Exit Do
            ol = l
            l *= 0.8
            w += ol / 2 + l / 2
        Loop
        Dim t = Scene.Add(New Text3D("Direct3D"))
        t.Material.Ambient = Color.AliceBlue
        t.Offset(0, 0, 0)
        'For x = -4 To 4 Step 0.25
        '    For y = -4 To 4 Step 0.25
        '        For z = -4 To 4 Step 0.25
        '            If (Abs(x * 4) + Abs(y * 4) + Abs(z * 4)) Mod 35 = 0 Then
        '                Dim s = Scene.Add(New Sphere(0.04))
        '                s.Offset(x, y, z)
        '                Scene.Shapes.Add(s)
        '            End If
        '        Next
        '    Next
        'Next
        For i = 0 To 1000
            Dim s = Scene.Add(New Sphere(0.04))
            s.Offset(-50 + Rnd(1) * 100, -50 + Rnd(1) * 100, -50 + Rnd(1) * 100)
            s.Material.Ambient = Color.White
            Scene.Shapes.Add(s)
        Next
        Show()
        Scene.InitializeGraphics(Me)
        Timer.Enabled = True
    End Sub

    'Sub CreateRabbit()
    '    Dim Text = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\pots.tri")
    '    Dim Shape As New Shape
    '    For Each Item In Split(Text, vbCrLf)
    '        Dim crd = Item.Split(" ")
    '        Dim p0 = New Point3d(crd(0), crd(1), crd(2), Color.FromArgb(crd(1) / 2 + 128, (crd(2) / 2 + 128), (crd(0) / 2 + 128)))
    '        Dim p1 = New Point3d(crd(3), crd(4), crd(5))
    '        Dim p2 = New Point3d(crd(6), crd(7), crd(8))
    '        Dim s As New Primitive
    '        s.p.Add(p0)
    '        s.p.Add(p1)
    '        s.p.Add(p2)
    '        Shape.Add(s)
    '    Next
    '    Scene.Shapes.Add(Shape)
    'End Sub

    Dim LastPoint As Point

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        MyBase.OnKeyPress(e)
        If e.KeyChar >= "1" And e.KeyChar <= "9" Then
            Scene.Shapes(Val(e.KeyChar) - 1).Visible = Not Scene.Shapes(Val(e.KeyChar) - 1).Visible
        End If
    End Sub

    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseDown(e)
        LastPoint = e.Location
    End Sub

    Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseMove(e)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If (ModifierKeys And Keys.Control) = 0 Then
                Scene.Camera.Rotate(-(e.Y - LastPoint.Y) / 400, (e.X - LastPoint.X) / 400)
            Else
                Scene.Camera.Offset((e.X - LastPoint.X), -(e.Y - LastPoint.Y), 0)
            End If
            LastPoint = e.Location
        End If
    End Sub

    Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Up Then
            Scene.Camera.Rotate(0.1, 0)
        ElseIf keyData = Keys.Down Then
            Scene.Camera.Rotate(-0.1, 0)
        ElseIf keyData = Keys.Left Then
            Scene.Camera.Rotate(0, -0.1)
        ElseIf keyData = Keys.Right Then
            Scene.Camera.Rotate(0, 0.1)
        End If
        Return MyBase.ProcessDialogKey(keyData)
    End Function

    Protected Overrides Sub OnMouseWheel(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseWheel(e)
        Scene.Camera.Zoom(1 - 0.1 * Sign(e.Delta))
    End Sub

    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        Scene.ResetDevice()
    End Sub

    Private Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer.Tick
        Static Busy As Boolean = False
        If Busy Then Exit Sub
        Busy = True
        Scene.Render(ClientRectangle.Width, ClientRectangle.Height)
        Application.DoEvents()
        Busy = False
    End Sub

    Private Sub MnuSolid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuSolid.Click
        Scene.View = Microsoft.DirectX.Direct3D.FillMode.Solid
    End Sub

    Private Sub MnuWireframe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuWireframe.Click
        Scene.View = Microsoft.DirectX.Direct3D.FillMode.WireFrame
    End Sub

    Private Sub MnuPoints_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuPoints.Click
        Scene.View = Microsoft.DirectX.Direct3D.FillMode.Point
    End Sub

    Private Sub MnuAliasing0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuAliasing0.Click
        Scene.Aliasing = Microsoft.DirectX.Direct3D.MultiSampleType.None
    End Sub

    Private Sub MnuAliasing2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuAliasing2.Click
        Scene.Aliasing = Microsoft.DirectX.Direct3D.MultiSampleType.TwoSamples
    End Sub

    Private Sub MnuAliasing4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuAliasing4.Click
        Scene.Aliasing = Microsoft.DirectX.Direct3D.MultiSampleType.FourSamples
    End Sub

    Private Sub MnuAliasing8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuAliasing8.Click
        Scene.Aliasing = Microsoft.DirectX.Direct3D.MultiSampleType.EightSamples
    End Sub
End Class




